//
//  SignNameView.m
//  计量检定现场记录
//
//  Created by 陈维华 on 16/5/13.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "SignNameView.h"
#import "ColorWithHex.h"
//颜色部分
#define colorWithBackground             [ColorWithHex hexStringToColor:@"#efeff4"]  //通用背景色
#define colorWithLine                   [ColorWithHex hexStringToColor:@"#e7e7e7"]  //线条颜色
#define colorWithBtnBackground          [ColorWithHex hexStringToColor:@"#56b3f5"]  //按钮背景色
#define colorWithBtnHeightBackground    [ColorWithHex hexStringToColor:@"#4ea1dc"]  //按钮高亮色
#define colorWithGrayLab                [ColorWithHex hexStringToColor:@"#aaaaaa"]  //灰色文本
#define colorWithBlackLab               [ColorWithHex hexStringToColor:@"#333333"]  //黑色字体色
#define ColorWithTextTag                [ColorWithHex hexStringToColor:@"#666666"]//次要信息,状态标识等,如页內tab
#define ColorWithMainBlue               [ColorWithHex hexStringToColor:@"#56b3f5"]// 主体蓝
#define ColorWithIconTitle              [ColorWithHex hexStringToColor:@"#797979"]//副标题颜色

//weakSelf
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;

//比例
#define lineScale                       (1/[[UIScreen mainScreen] scale])    //线条1px

@interface SignNameView()

@property (nonatomic, assign) CGRect frame;
@end

@implementation SignNameView

- (instancetype)initWithFrame:(CGRect)frame {
  if (self = [super init]) {
      self.frame = frame;
    // 背景
    self.maskView = [[UIView alloc]  init];
    self.maskView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.553];
    self.maskView.alpha = 0.1f;
    [self.maskView addGestureRecognizer:[[UITapGestureRecognizer alloc]
                                            initWithTarget:self
                                                    action:@selector(dismiss)]];
    self.maskView.translatesAutoresizingMaskIntoConstraints = NO;
      
      
    self.ContentView = [[UIView alloc] init];
    [self.ContentView setBackgroundColor:[UIColor colorWithWhite:1.000 alpha:0.000]];
    self.ContentView.translatesAutoresizingMaskIntoConstraints = NO;

      
    UIView *signNameBg = [[UIView alloc] init];
    signNameBg.backgroundColor =  ColorWithMainBlue;// [UIColor colorWithHexString:@"56b3f5"];
//    signNameBg.image = [UIImage imageNamed:@"signName"];
//    [signNameBg setContentMode:UIViewContentModeScaleAspectFill];
//    signNameBg.clipsToBounds = YES;
    [self.ContentView addSubview:signNameBg];
    [signNameBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.ContentView);
        make.centerX.equalTo(self.ContentView);
        make.height.equalTo(@115);
//        make.width.equalTo(@153);
    }];

      UIImageView *bgView = [[UIImageView alloc] init];
      bgView.image = [UIImage imageNamed:@"signName"];
      [signNameBg addSubview:bgView];
      [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
          make.width.equalTo(@153);
          make.height.equalTo(@88);
          make.bottom.equalTo(signNameBg);
          make.centerX.equalTo(signNameBg);
      }];
      
    self.signName = [[SignName alloc] init];
    self.signName.translatesAutoresizingMaskIntoConstraints = NO;
    self.signName.backgroundColor = [UIColor whiteColor];
    self.signName.delegate = self;
    self.signName.showMessage = @""; //完成
    [self.ContentView addSubview:self.signName];
    self.signName.layer.masksToBounds = YES;
    self.signName.layer.cornerRadius = 0.00;
    [self.signName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.ContentView);
        make.top.equalTo(self.ContentView).mas_offset(115);
        make.bottom.equalTo(self.ContentView.mas_bottom).mas_offset(0);
    }];
      
      WS(weakSelf);
      self.signName.block = ^(void){
          if (weakSelf.block) {
              weakSelf.block();
              return;
          }
      };
 

   _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelBtn.translatesAutoresizingMaskIntoConstraints = NO;
    [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelBtn setTitleColor:ColorWithMainBlue forState:UIControlStateNormal];

    _cancelBtn.layer.cornerRadius = 20.0;
    _cancelBtn.clipsToBounds = YES;
    _cancelBtn.layer.borderWidth = lineScale;
      _cancelBtn.layer.borderColor = ColorWithMainBlue.CGColor;
    _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:17];
      _cancelBtn.backgroundColor = [UIColor whiteColor];
    [_cancelBtn addTarget:self
                  action:@selector(cancel:)
        forControlEvents:UIControlEventTouchUpInside];
    [self.ContentView addSubview:_cancelBtn];
    [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@158);
        make.height.equalTo(@40);
        make.left.equalTo(self.ContentView).mas_offset(10);
        make.bottom.equalTo(self.ContentView).mas_offset(-20);
    }];
   

    _tureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_tureBtn setTitle:@"确认" forState:UIControlStateNormal];
    [_tureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _tureBtn.titleLabel.font = [UIFont systemFontOfSize:17];
      _tureBtn.backgroundColor = ColorWithMainBlue;
    _tureBtn.translatesAutoresizingMaskIntoConstraints = NO;
    _tureBtn.layer.cornerRadius = 20.0;
    _tureBtn.clipsToBounds = YES;
    _tureBtn.layer.borderWidth = lineScale;
    _tureBtn.layer.borderColor = ColorWithMainBlue.CGColor;
    [_tureBtn addTarget:self
                  action:@selector(add:)
        forControlEvents:UIControlEventTouchUpInside];
    [self.ContentView addSubview:_tureBtn];
      
      [_tureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
          make.width.equalTo(@158);
          make.height.equalTo(@40);
          make.right.equalTo(self.ContentView).mas_offset(-10);
          make.bottom.equalTo(self.ContentView).mas_offset(-20);
      }];
      
      //清除
      _resignBtn = [UIButton buttonWithType:UIButtonTypeCustom];
      _resignBtn.translatesAutoresizingMaskIntoConstraints = NO;
      [_resignBtn setTitle:@"清除" forState:UIControlStateNormal];
      [_resignBtn setTitleColor:ColorWithMainBlue forState:UIControlStateNormal];
      
    
      _resignBtn.titleLabel.font = [UIFont systemFontOfSize:14];
      _resignBtn.backgroundColor = [UIColor whiteColor];
      [_resignBtn addTarget:self
                     action:@selector(clear:)
           forControlEvents:UIControlEventTouchUpInside];
      [self.ContentView addSubview:_resignBtn];
      [_resignBtn mas_makeConstraints:^(MASConstraintMaker *make) {
          make.width.height.equalTo(@44);
          make.right.equalTo(self.ContentView).mas_offset(-10);
          make.top.equalTo(signNameBg.mas_bottom).mas_offset(10);
      }];
  }

  return self;
}

- (void)cancel:(UIButton *)sender{
    [self dismiss];
}

- (void)add:(UIButton *)sender {
  [self.signName sure];
}

- (void)clear:(UIButton *)sender {
  NSLog(@"重签");
  [self.signName clear];

  for (UIView *view in saveView.subviews) {
    [view removeFromSuperview];
  }
}

- (void)getSignatureImg:(UIImage *)image {
  [self dismiss];
  if (image) {
    NSLog(@"haveImage");

//    UIImageView *image1 = [[UIImageView alloc] initWithImage:image];
//    image1.frame =
//        CGRectMake((saveView.frame.size.width - image.size.width) / 2,
//                   (saveView.frame.size.height - image.size.height) / 2,
//                   image.size.width, image.size.height);
    //        [saveView  addSubview:image1];

//    saveImage = image;
    self.getSignNameImage(image);

//    [self saveImage:saveImage];
    //[self makeUpLoad];

  } else {
    NSLog(@"NoImage");
  }
}


//图片保存到本地
//- (void)saveImage:(UIImage *)image {
//  //设置图片名
//  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//  [dateFormatter setDateFormat:@"yyyyMMdd"];
//  NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
//  NSString *dateStr = [NSString stringWithFormat:@"%@.png", currentDateStr];
//  NSString *path =
//      [NSTemporaryDirectory() stringByAppendingFormat:@"%@", dateStr];
//  //    BOOL existed = [[NSFileManager defaultManager] fileExists
//  //    AtPath:pathisDirectory:nil];
//  NSLog(@"%@", path);
//
//  BOOL existed =
//      [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:nil];
//
//  if (existed) {
//    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
//  }
//  NSData *imgData = UIImageJPEGRepresentation(image, 1);
//  [imgData writeToFile:path atomically:YES];
//}

- (void)show {
  UIWindow *window = [UIApplication sharedApplication].keyWindow;


  [window addSubview:self.maskView];
  [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.center.equalTo(window);
      make.edges.equalTo(window);
  }];
  [window addSubview:self.ContentView];
   [self.ContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(window).mas_offset(10);
        make.right.equalTo(window).mas_offset(-10);
        make.center.equalTo(window);
//        make.width.equalTo(@356);
        make.height.equalTo(@461);
    }];


  self.ContentView.alpha = 0.0f;
  self.maskView.alpha = .0f;

  [UIView animateWithDuration:0
      delay:0
      options:UIViewAnimationOptionCurveEaseInOut
      animations:^{
        [self.ContentView.layer setValue:@(0) forKeyPath:@"transform.scale"];
      }
      completion:^(BOOL finished) {
        [UIView animateWithDuration:0.30
            delay:0
            options:UIViewAnimationOptionCurveEaseInOut
            animations:^{
              [self.ContentView.layer setValue:@(1.2)
                                    forKeyPath:@"transform.scale"];
            }
            completion:^(BOOL finished) {
              [UIView animateWithDuration:0.29
                  delay:0.02
                  options:UIViewAnimationOptionCurveEaseInOut
                  animations:^{
                    [self.ContentView.layer setValue:@(.9)
                                          forKeyPath:@"transform.scale"];
                  }
                  completion:^(BOOL finished) {
                    [UIView animateWithDuration:0.05
                        delay:0.02
                        options:UIViewAnimationOptionCurveEaseInOut
                        animations:^{
                          [self.ContentView.layer setValue:@(1.0)
                                                forKeyPath:@"transform.scale"];
                        }
                        completion:^(BOOL finished){

                        }];
                  }];
            }];
      }];

  [UIView animateWithDuration:.35
                   animations:^{
                     self.ContentView.alpha = 1.0;
                     //        self.contentView.transform =
                     //        CGAffineTransformMakeScale(1.0, 1.0);
                     self.maskView.alpha = .5f;
                   }];
}
- (void)dismiss {
  [UIView animateWithDuration:.15
      animations:^{
        //        self.contentView.transform = CGAffineTransformMakeScale(.5f,
        //        .5f);
        self.ContentView.alpha = 0.0f;
        self.maskView.alpha = .0f;
      }
      completion:^(BOOL finished) {
        if (finished) {
          [self.maskView removeFromSuperview];
          [self.ContentView removeFromSuperview];
        }
      }];
}

@end
